<template>
  <div>
    <nav-top class="nav"
             Licon="back"
             @callback="$router.go(-1)"></nav-top>

    <div class="add gl-content">
      <div class="content ">
        <form class="myform">
          <div>
            <label>姓名:</label><input type="text"
                   v-model="item_name" />
          </div>
          <div>
            <label>电话:</label><input type="text"
                   v-model="item_phone" />
          </div>
          <div>
            <label>账号:</label><input type="text"
                   ref="item_account"
                   placeholder="可输入2-20字符内字母数字"
                   maxlength="20"
                   v-model="item_account" />
          </div>

          <div class="quotasVip">
            <label>额度:</label>
            <input type="text"
                   v-model="quota"
                   :placeholder="'最多可分配'+quotaNum" />
          </div>
          <div><label>备注:</label></div>
          <div class="tarea">
            <textarea v-model="textArea"></textarea>
          </div>
        </form>
        <div class="tips">
          <p>注：首次添加账号，默认密码12345678。</p>
        </div>
        <div class="btn">
          <button class="cancel"
                  @click="$router.go(-1)">取消</button>
          <button class="submission"
                  @click="sub">提交</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapMutations, mapGetters } from 'vuex'
export default {
  name: 'IeamAdd',
  components: {},
  data() {
    return {
      item_account: '', // 账号
      quota: '', // 额度
      item_name: '', // 姓名
      item_phone: '', // 手机号
      textArea: '' ,// 备注
      quotaNum:'',// 会员额度
    }
  },
  computed: {
    ...mapGetters(['proxy_id'])
  },
  watch: {
    item_account() {
      //   console.log(this.item_account);
      // 判断账号是否是字母和数字
      let account = /^[a-z0-9]+$/i
      if (!account.test(this.item_account) && this.item_account) {
        this.$toast({
          message: '只能输入2-20字符内字母数字',
          duration: 1500
        })
        this.item_account = ''
      }
    },
    quota() {
      // 判断额度是否超出
      if (this.quota > this.quotaNum) {
        // console.log("最多可分配547");
        this.$toast({
          message: '最多可分配'+this.quotaNum,
          duration: 1500
        })
        this.quota = this.quotaNum
      }
    }
  },
  methods: {
    // 请求分配额度
    getQuota() {
      let params = {
        proxy_id: this.proxy_id
      }
      this.Http.getAllotList(params).then(res => {
      
        // console.log(res);
        this.quotaNum = res.can_num;
      
      })
    },
    // 点击提交
    sub() {
      // 判断手机号码
      // let isPhone = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/;

      this.addData()
    },
    // 提交新增数据
    addData() {
      let params = {
        proxy_id: this.proxy_id,
        name: this.item_name,
        phone: this.item_phone,
        username: this.item_account,
        vip_num: this.quota,
        remark: this.textArea
      }
      let times
      clearTimeout(times)
      // console.log(params);
      this.Http.addItem(params, { resDataKey: '' })
        .then(res => {
          // console.log(res);
          this.$toast({
            message: '添加成功',
            duration: 500
          })
          times = setTimeout(() => {
            this.$router.push('/admin/team/index')
          }, 1500)
        })
        .catch(err => {
          // console.log(err);
          if (err.msg == '队员账号格式不正确') {
            this.$toast({
              message: '账号至少输入2位字符',
              duration: 1500
            })
          } else {
            this.$toast({
              message: err.msg,
              duration: 1500
            })
          }
        })
    }
  },
  created() {
      this.getQuota();
  },
  mounted() {}
}
</script>
<style lang="less" scoped>
.add {
  padding-top: 0px;
  padding-bottom: 64px;
  min-height: calc(100vh - 175px);
}
.content {
  margin: 0 auto;
  width: 678px;
  height: 841px;
  background-color: #fff;
  box-shadow: 0px 6px 18px 0px rgba(0, 0, 0, 0.1);
  border-radius: 20px;

  .myform {
    box-sizing: border-box;
    padding: 0 34px;
    font-size: 34px;
    div {
      display: flex;
      align-items: center;
      line-height: 87px;
      // justify-content: space-between;
      label {
        width: 77px;
        font-size: 34px;
        font-family: PingFang SC;
        font-weight: 500;
        color: rgba(119, 119, 119, 1);
        padding-right: 40px;
      }

      input {
        height: 52px;
        font-size: 34px;
        color: #222222;
        font-family: PingFang SC;
        font-weight: 500;
        border: 1px solid rgba(236, 236, 236, 1);
        border-radius: 6px;
      }
    }

    .tarea {
      margin: 0 auto;
      textarea {
        width: 620px;
        height: 170px;
        resize: none;
        font-size: 34px;
        border: 1px solid rgba(236, 236, 236, 1);
        border-radius: 6px;
        border: 1px solid rgba(236, 236, 236, 1);
      }
    }
  }
  .tips {
    text-align: center;
    font-size: 30px;
    font-family: PingFang SC;
    font-weight: 500;
    color: rgba(119, 119, 119, 1);
    margin-top: 34px;
  }
  .btn {
    display: flex;
    justify-content: space-around;
    align-items: center;
    margin-top: 40px;
    button {
      width: 290px;
      height: 90px;
      border-radius: 20px;
      font-size: 32px;
      font-family: PingFang SC;
      font-weight: 500;
      color: rgba(255, 255, 255, 1);
    }
    .cancel {
      background: linear-gradient(
        -83deg,
        rgba(255, 133, 122, 1),
        rgba(255, 81, 66, 1)
      );
      box-shadow: 0px 7px 12px 0px rgba(255, 117, 105, 0.4);
    }
    .submission {
      background: linear-gradient(
        -83deg,
        rgba(20, 200, 255, 1),
        rgba(2, 151, 255, 1)
      );
      box-shadow: 0px 7px 12px 0px rgba(79, 184, 254, 0.4);
    }
  }
}
.nav {
  background-color: #f7f8fa;
}
// input提示语
::-webkit-input-placeholder {
  /* WebKit, Blink, Edge */
  font-size: 24px;
  font-family: PingFang SC;
  font-weight: 400;
  color: rgba(186, 186, 186, 1);
  line-height: 52px;
}
:-moz-placeholder {
  /* Mozilla Firefox 4 to 18 */
  font-size: 24px;
  font-family: PingFang SC;
  font-weight: 400;
  color: rgba(186, 186, 186, 1);
  opacity: 1;
}
::-moz-placeholder {
  /* Mozilla Firefox 19+ */
  font-size: 24px;
  font-family: PingFang SC;
  font-weight: 400;
  color: rgba(186, 186, 186, 1);
  opacity: 1;
}
:-ms-input-placeholder {
  /* Internet Explorer 10-11 */
  font-size: 24px;
  font-family: PingFang SC;
  font-weight: 400;
  color: rgba(186, 186, 186, 1);
}
</style>
